#!/usr/bin/node

const log = console.log,  
      fs = require('fs'),
      http = require('http'),
      path = require('path'),
      file = process.argv[2];
if(process.argv.length !== 3){
  console.error('命令行参数格式：cmd fileName');
  process.exit(1);
}
try{
  var data = fs.readFileSync(file).toString('base64');
}catch(e){
  console.error(e.message);
  process.exit(2);
}
let ext = path.extname(file);
let uriData = 'data:image/' + ext.slice(1, ext.length) + ';base64,' + data;
let html = '<!DOCTYPE html> <html><body><img alt="'
         + path.basename(file, ext)
         + '" src="' +uriData + '"></body></html>';
http.createServer((req, res) => {
  log(req.headers);
  log(req.url + '\n');
  res.end(html);
}).listen(8080);

//let data = fs.readFileSync(pic);
//let uri = data.toString('base64');

//log(`uri: ${uri}`);

//let html = `<html>
//            <head>        
//                <title>data uri</title>
//            </head>
//            <body>
//                  <img src="data:image/jpg;base64,${uri}">
//            </body>
//            </html>`;

//http.createServer((req, res)=>{
//     res.end(html);
//}).listen(8080);
